define(["require","dojo/_base/declare", "../BaseWidget",
        "dojo/_base/lang","dojo/on","dojo/dom-class",  "dojo/_base/event","dojo/topic",
        "esri/toolbars/draw","dojo/_base/event",
        "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", 
        "esri/symbols/SimpleFillSymbol","esri/symbols/TextSymbol", "esri/graphic",
        "esri/Color", "esri/toolbars/edit",
        "esri/geometry/webMercatorUtils","esri/geometry/Point",
        "dojo/text!./templates/Template.html"],
        function(require,declare,BaseWidget,lang,on,domClass,event,topic,Draw,Event,SimpleMarkerSymbol, SimpleLineSymbol,
        		SimpleFillSymbol, TextSymbol,Graphic,Color,Edit,webMercatorUtils,Point,template) {
	return declare([BaseWidget], {
		//模板页面
		templateString : template,
		map:null,
		handles:[],
		drawtools:null,
		currentDrawType:"",
		symbols:{},
		temphandlers:[],
		currentDrawGraphic:null,
		editToolbar:null,
		allowEidt:false,	
		landExpandManageWidget:null,
		constructor:function(args) {
			//debugger;
			this.inherited(arguments);
			this.map = args.map;	
			
		},
		//组件初始化方法
		postCreate : function() {
			this.inherited(arguments);
			dojo.place(this.domNode, dojo.body());
		},
		destroy:function(){
			this.inherited(arguments);
			this.map = null;
		},
		startup:function(){
			this.inherited(arguments);
			this.init();
			this.addEvents();
		},
		init:function(){
			this.drawtools = new Draw(this.map);
			this.symbols.point = new SimpleMarkerSymbol("circle", 10, new SimpleLineSymbol(), new Color([255, 0, 0, 0.75]));
			this.symbols.polyline = new SimpleLineSymbol("solid", new Color([255, 0, 0]), 2);
			this.symbols.polygon = new SimpleFillSymbol().setColor(new Color([255,0,0,0.25]));
			this.symbols.extent = new SimpleFillSymbol().setColor(new Color([255,0,0,0.25]));
			//注册组件是添加的监听事件列表，关闭时移除监听，以避免重复
			handles = [on(this.drawtools,"draw-end", lang.hitch(this,this.addFeatureToMap))];
			this.editToolbar = new Edit(this.map);		
			
		},
		addEvents:function(){
			//激活绘制
			dojo.query(".toggoleselect").map(lang.hitch(this,function(item,index){
				dojo.connect(item,"onclick",lang.hitch(this,function(){  
					//debugger
					this.reSetAndDeactivate();
					dojo.toggleClass(item, "drawbtnselect",true);
					//激活绘制
					if(domClass.contains(item, "drawing")){
						//debugger
						this.activateTool(item);
					}else{
						this.allowEidt = true;
						this.activateEditToolbar(this.currentDrawGraphic);
					}
				}));  
			}));
			on(this.clearmap, "click",lang.hitch(this,this.ClearDrawGraphicLayer));
			on(this.closebtn, "click",lang.hitch(this,this.CloseDrawToolWidget));
			on(this.map,"click",lang.hitch(this,this.DeactiveEditbar));
			this.editToolbar.on("deactivate", function(evt) {
				//debugger
			   if(Global.currentDraw=="landExpandManager"){
				   topic.publish("landExpandManagereditGeoed",{geo:evt.graphic})
			   }
			   if(Global.currentDraw=="landExpandManager"){
				   topic.publish("landNoteDrawed",{geo:evt.graphic})
			   }
            });
			topic.subscribe("LandExpandManageWidgetDestroy",lang.hitch(this,this.CloseDrawToolWidget));
			//书签关闭时触发
			topic.subscribe("BookMarkDestory",lang.hitch(this,this.CloseDrawToolWidget));
			topic.subscribe("LandNoteWidgetDestroy",lang.hitch(this,this.CloseDrawToolWidget));
			
			this.allowEidtGraphic();
		},
		DeactiveEditbar:function(){
			this.editToolbar.deactivate();
		},
		reSetAndDeactivate:function(){
			//切换按钮样式
			dojo.query(".toggoleselect").map(function(item,index){
				 dojo.toggleClass(item, "drawbtnselect",false);
			});
			if(this.drawtools){
				//debugger
				this.drawtools.deactivate();
			}
			 if(this.editToolbar){
				// debugger
				  this.editToolbar.deactivate();
				  this.allowEidt = false;
			 }
		},
		allowEidtGraphic:function () {	
			handles.push(on(this.map, "click",lang.hitch(this,function(evt){
				if(this.editToolbar&&this.allowEidt){
					this.editToolbar.deactivate();
				}
			})));
		},
		activateEditToolbar:function (graphic) {
			var tool = 0;
			tool = tool | Edit.MOVE; 
			tool = tool | Edit.EDIT_VERTICES; 
			var options = {
					allowAddVertices: true,
					allowDeleteVertices: true,
					uniformScaling: true
			};
			this.editToolbar.activate(tool, graphic, options);
		},
		ClearDrawGraphicLayer:function(){
			this.map.graphics.clear();
			this.reSetAndDeactivate();
		},
		
		//激活相应的地图绘制
		activateTool:function(itemobj) {
//			this.map.graphics.clear();
			this.currentDrawType = itemobj.id.replace("freehand","");
			this.drawtools.activate(itemobj.id.replace("text",""));
			this.map.setInfoWindowOnClick(false);
			
		},
		//将绘制结束后得到的几何图形叠加在地图上显示
		addFeatureToMap:function (evt) {
			//debugger
			this.drawtools.deactivate();
			var drawGraphic = new Graphic(evt.geometry, this.symbols[this.currentDrawType]);
			this.currentDrawGraphic = drawGraphic;
			this.map.graphics.add(drawGraphic);
			this.map.setInfoWindowOnClick(true);
			if(Global.currentDraw=="landExpandManager"){
				   topic.publish("landExpandManagereditGeoed",{geo:this.currentDrawGraphic})
			   }
			   if(Global.currentDraw=="landNote"){
				   topic.publish("landNoteDrawed",{geo:this.currentDrawGraphic})
			   }
		},
		CloseDrawToolWidget:function(){
			this.inherited("closeDialog",arguments);
//			this.map.graphics.remove(this.currentDrawGraphic);
//			this.map.graphics.clear();
			if(Global.widgets.mapWidget.map.graphics.graphics.length>0){
				Global.widgets.mapWidget.map.graphics.clear();
			}
			dojo.forEach(handles, function(handle, i){
				handle.remove();//移除监听
			});
			this.destroy();
		}
	});
});